﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Security.Principal;

using MSAP.Framework.Interfaces;
using MSAP.Framework;

using PLL.Shell;
using PLL.Accueil;
using PLL.Notes;

namespace XBAPSample
{
    /// <summary>
    /// Interaction logic for Shell.xaml
    /// </summary>
    public partial class ShellPage : Page
    {
        public ShellPage()
        {
            InitializeComponent();
            
            //On initialise le Shell
            IShell shell = ShellModuleInitializer.CreateModule(null) as IShell;
            this.DataContext = shell;

            InitShell();
        }

        private void InitShell()
        {
            ShellModule shellModule = Shell as ShellModule;
            Shell.Init();
            if (shellModule.LoginStatus == eLoginStatus.Connected)
            {
                //On ajoute les modules de l'application.
                Shell.AddModule(PLL.Accueil.ModuleInitializer.CreateModule(Shell));
                Shell.AddModule(PLL.Notes.ModuleInitializer.CreateModule(Shell));

                //On ajoute manuellement ici le service accueil dans la liste des Workplace.
                AccueilModule accueilModule = Shell.GetModule("ACCUEIL") as AccueilModule;
                AccueilServicePage accueilServicePage = new AccueilServicePage(accueilModule);
                accueilModule.AddServicePage(accueilServicePage);
                Shell.DefaultServicePage = accueilServicePage;

                InitMainMenu();

                this.tabControl.Visibility = Visibility.Visible;
                this.tbxRole.Visibility = Visibility.Visible;
                this.btnLogout.Visibility = Visibility.Visible;
                this.RefreshConnexion.Visibility = Visibility.Collapsed;
                this.tabControl.ItemContainerGenerator.StatusChanged += new EventHandler(ItemContainerGenerator_StatusChanged);

                this.tbkLogin.Text = shellModule.LoginName;
            }
            else
            {
                switch (shellModule.LoginStatus)
                {
                    case eLoginStatus.Refused:
                        this.tbkLogin.Text = "Connexion refusé";
                        break;
                    default:
                        this.tbkLogin.Text = "Login non testé";
                        break;
                }
                this.tbxRole.Visibility = Visibility.Collapsed;
                this.btnLogout.Visibility = Visibility.Collapsed;
                this.tabControl.Visibility = Visibility.Hidden;
                this.RefreshConnexion.Visibility = Visibility.Visible;
            }
        }

        void ItemContainerGenerator_StatusChanged(object sender, EventArgs e)
        {
            if ((sender as ItemContainerGenerator).Status == System.Windows.Controls.Primitives.GeneratorStatus.ContainersGenerated && this.tabControl.SelectedIndex == -1)
            {
                this.tabControl.SelectedIndex = 0;
            }
        }

        IShell Shell
        {
            get { return this.DataContext as IShell; }
        }

        /// <summary>
        /// Initialisation du main menu en se basant sur le Shell
        /// </summary>
        protected void InitMainMenu()
        {
            Shell.View = this.tabControl;

            MenuItem menuRoot = new MenuItem();
            menuRoot.Header = Shell.Title;
            if (Shell.Icon != null)
            {
                Image icon = new Image();
                icon.Source = Shell.Icon;
                icon.Width = 20;
                icon.Height = 20;
                menuRoot.Icon = icon;
            }
            this.MainMenu.Items.Add(menuRoot);

            InitModuleMenu(Shell, menuRoot);
        }

        /// <summary>
        /// Initialisation du menu "modules" en parcourant les modules inscrits dans le Shell
        /// </summary>
        /// <param name="shell">le shell</param>
        /// <param name="item">Le menu item de niveau inférieur (root)</param>
        protected void InitModuleMenu(IShell shell, MenuItem item)
        {
            foreach (IModule module in shell.Modules)
            {
                // create module item
                MenuItem menuItem = new MenuItem();
                menuItem.Header = module.Title;

                if (module.Icon != null)
                {
                    Image icon = new Image();
                    icon.Source = module.Icon;
                    icon.Width = 25;
                    icon.Height = 25;
                    menuItem.Icon = icon;
                }
                item.Items.Add(menuItem);
                
                // Creation action for module item
                InitActionMenu(module, menuItem);
            }
        }

        /// <summary>
        /// Initialisation des services (sous modules / menus) inscrits dans chacun des modules. Il s'agit de serviceAction
        /// </summary>
        /// <param name="module"></param>
        /// <param name="item"></param>
        protected void InitActionMenu(IModule module, MenuItem item)
        {
            foreach (IServiceAction service in module.ServiceActions)
            {
                MenuItem menuItem = new MenuItem();
                menuItem.Header = service.Title;
                menuItem.Command = service.Command;

                if (service.Icon != null)
                {
                    Image icon = new Image();
                    icon.Source = service.Icon;
                    icon.Width = 16;
                    icon.Height = 16;
                    menuItem.Icon = icon;
                }
                item.Items.Add(menuItem);
            }
        }

        private void RefreshConnexion_Click(object sender, RoutedEventArgs e)
        {
            InitShell();
        }
    }
}
